package com.wskj.web.controller.workbench;

import com.wskj.common.annotation.Log;
import com.wskj.common.annotation.RepeatSubmit;
import com.wskj.common.core.controller.BaseController;
import com.wskj.common.core.domain.AjaxResult;
import com.wskj.common.core.page.TableDataInfo;
import com.wskj.common.enums.BusinessType;
import com.wskj.common.exception.CustomException;
import com.wskj.common.utils.poi.ExcelUtil;
import com.wskj.workbench.bo.web.TbUserReceiveAddBo;
import com.wskj.workbench.bo.web.TbUserReceiveEditBo;
import com.wskj.workbench.bo.web.TbUserReceiveQueryBo;
import com.wskj.workbench.service.ITbUserReceiveService;
import com.wskj.workbench.vo.web.TbUserReceiveVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 客户收货地址信息Controller
 *
 * @author wsdz
 * @date 2022-09-26
 */
@Api(value = "客户收货地址信息控制器", tags = {"客户收货地址信息管理"})
@AllArgsConstructor
@RestController
@RequestMapping("/user/receive")
public class TbUserReceiveController extends BaseController {

    private final ITbUserReceiveService iTzUserReceiveService;

    /**
     * 查询客户收货地址信息列表
     */
    @ApiOperation("查询客户收货地址信息列表")
    @PreAuthorize("@ss.hasPermi('mall:receive:list')")
    @GetMapping("/list")
    public TableDataInfo<TbUserReceiveVo> list(@Validated TbUserReceiveQueryBo bo) {
        return iTzUserReceiveService.queryPageList(bo);
    }

    /**
     * 导出客户收货地址信息列表
     */
    @ApiOperation("导出客户收货地址信息列表")
    @PreAuthorize("@ss.hasPermi('mall:receive:export')")
    @Log(title = "客户收货地址信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<Void> export(@Validated TbUserReceiveQueryBo bo) {
        List<TbUserReceiveVo> list = iTzUserReceiveService.queryList(bo);
        ExcelUtil<TbUserReceiveVo> util = new ExcelUtil<>(TbUserReceiveVo.class);
        return util.exportExcel(list, "客户收货地址信息");
    }

    /**
     * 获取客户收货地址信息详细信息
     */
    @ApiOperation("获取客户收货地址信息详细信息")
    @PreAuthorize("@ss.hasPermi('mall:receive:query')")
    @GetMapping("/{receiveId}")
    public AjaxResult<TbUserReceiveVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("receiveId") Long receiveId) {
        return AjaxResult.success(iTzUserReceiveService.queryById(receiveId));
    }

    /**
     * 新增客户收货地址信息
     */
    @ApiOperation("新增客户收货地址信息")
    @PreAuthorize("@ss.hasPermi('mall:receive:add')")
    @Log(title = "客户收货地址信息", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody TbUserReceiveAddBo bo) {
        return toAjax(iTzUserReceiveService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改客户收货地址信息
     */
    @ApiOperation("修改客户收货地址信息")
    @PreAuthorize("@ss.hasPermi('mall:receive:edit')")
    @Log(title = "客户收货地址信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated @RequestBody TbUserReceiveEditBo bo) {
        return toAjax(iTzUserReceiveService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除客户收货地址信息
     */
    @ApiOperation("删除客户收货地址信息")
    @PreAuthorize("@ss.hasPermi('mall:receive:remove')")
    @Log(title = "客户收货地址信息" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{receiveIds}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] receiveIds) {
        return toAjax(iTzUserReceiveService.deleteWithValidByIds(Arrays.asList(receiveIds), true) ? 1 : 0);
    }

    /**
     * 导入客户收货地址信息数据
     */
    @ApiOperation("导入客户收货地址信息数据")
    @ApiImplicitParam(name = "file", value = "文件", required = true, dataType = "file")
    @PreAuthorize("@ss.hasPermi('mall:receive:import')")
    @Log(title = "客户收货地址信息" , businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult<Void> importData(MultipartFile file) {

        ExcelUtil<TbUserReceiveVo> util = new ExcelUtil<>(TbUserReceiveVo.class);
        List<TbUserReceiveVo> tzUserReceiveList;
        try {
            tzUserReceiveList = util.importExcel(file.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException("文件解析异常！message：" + e.getMessage());
        }
        String message = iTzUserReceiveService.importTzUserReceive(tzUserReceiveList);
        return AjaxResult.success(message);

    }

    /**
     * 下载模板
     *
     * @return AjaxResult 模板文件
     */
    @ApiOperation("下载模板")
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate() {
        ExcelUtil<TbUserReceiveVo> util = new ExcelUtil<>(TbUserReceiveVo.class);
        return util.importTemplateExcel("客户收货地址信息数据");
    }
}
